package com.guofen.service.sjq.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guofen.dao.SjqWaterProductionDeliverWaterMapper;
import com.guofen.pojo.SjqWaterProductionDeliverWater;
import com.guofen.service.sjq.SjqWaterProductionDeliverWaterService;
import com.guofen.util.DateAndTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SjqWaterProductionDeliverWaterServiceImpl implements SjqWaterProductionDeliverWaterService {
    @Autowired
    private SjqWaterProductionDeliverWaterMapper sjqWaterProductionDeliverWaterMapper;

    @Override
    public boolean addForm(Integer train_number1, Double weight1, String deliver_person, String recording_person, String remarks){
        //获取当前日期
        String date = DateAndTimeUtil.getDate();
        System.out.println(date);

        SjqWaterProductionDeliverWater sjqWaterProductionDeliverWater = new SjqWaterProductionDeliverWater();
        sjqWaterProductionDeliverWater.setDate(date);
        sjqWaterProductionDeliverWater.setTrainNumber(train_number1);
        sjqWaterProductionDeliverWater.setWeight(weight1);
        sjqWaterProductionDeliverWater.setDeliverPerson(deliver_person);
        sjqWaterProductionDeliverWater.setRecordingPerson(recording_person);
        sjqWaterProductionDeliverWater.setRemarks(remarks);
        int result = sjqWaterProductionDeliverWaterMapper.insert(sjqWaterProductionDeliverWater);
        if (result == 1)
            return true;
        else
            return false;
    }

    @Override
    public boolean addFormExcel(String date,Integer train_number1, Double weight1, String deliver_person, String recording_person, String remarks){
        SjqWaterProductionDeliverWater sjqWaterProductionDeliverWater = new SjqWaterProductionDeliverWater();
        sjqWaterProductionDeliverWater.setDate(date);
        sjqWaterProductionDeliverWater.setTrainNumber(train_number1);
        sjqWaterProductionDeliverWater.setWeight(weight1);
        sjqWaterProductionDeliverWater.setDeliverPerson(deliver_person);
        sjqWaterProductionDeliverWater.setRecordingPerson(recording_person);
        sjqWaterProductionDeliverWater.setRemarks(remarks);
        int result = sjqWaterProductionDeliverWaterMapper.insert(sjqWaterProductionDeliverWater);
        if (result == 1)
            return true;
        else
            return false;
    }

    @Override
    public List<SjqWaterProductionDeliverWater> findForm() {
        //获取当前日期
        String date = DateAndTimeUtil.getDate();
        System.out.println("当前日期" + date);
        //获取最新记录
        List<SjqWaterProductionDeliverWater> list = sjqWaterProductionDeliverWaterMapper.findLast();
        System.out.println("最新记录" + sjqWaterProductionDeliverWaterMapper.findLast());
        //获取最新记录日期
        String dateLast = new String();
        for(SjqWaterProductionDeliverWater sjqWaterProductionDeliverWater:list){
            dateLast = sjqWaterProductionDeliverWater.getDate();
            System.out.println("最新记录日期" + dateLast);
        }
        if (date.equals(dateLast))
            return list;
        else
            return null;
    }

    @Override
    public Map<String, Object> get(int pageSize, int currentPage) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        List<SjqWaterProductionDeliverWater> list = sjqWaterProductionDeliverWaterMapper.selectAll();
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public Map<String, Object> getByTime(int pageSize, int currentPage, long startTime, long endTime) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String startDate = dateFormat.format(new Date(startTime));
        String endDate = dateFormat.format(new Date(endTime));
        List<SjqWaterProductionDeliverWater> list = sjqWaterProductionDeliverWaterMapper.selectByDate(startDate, endDate);
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public int delete(int id) {
        return sjqWaterProductionDeliverWaterMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(SjqWaterProductionDeliverWater record) {
        return sjqWaterProductionDeliverWaterMapper.updateByPrimaryKeySelective(record);
    }
}
